package com.shujia.bigdata.student;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;

public class Reduce {
    public static void main(String[] args) throws Exception {

        /**
         * 读取临时目录的数据，汇总结果
         *
         */

        File file = new File("java/data/stu_tmp");


        File[] files = file.listFiles();

        if (files == null) {
            return;
        }


        HashMap<String, Long> map = new HashMap<>();

        for (File file1 : files) {

            FileReader fileReader = new FileReader(file1);
            BufferedReader bufferedReader = new BufferedReader(fileReader);


            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] split = line.split(",");
                String clazz = split[0];
                Long count = Long.parseLong(split[1]);

                Long aLong = map.get(clazz);

                if (aLong == null) {
                    map.put(clazz, count);
                } else {
                    map.put(clazz, count + aLong);
                }
            }
        }

        System.out.println(map);


    }
}
